package com.example.demo.util;

import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * @author
 * @since 2019—10-30
 */
@Component
@Slf4j
public class RedisSentinelUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    private ValueOperations<String, String> valueOperations;

    @PostConstruct
    public void init() {
        valueOperations = stringRedisTemplate.opsForValue();
    }

    public void setValue(String key, String value) {

        valueOperations.set(key, value);
        log.info("设置k : {} , v : {} ", key, value);
    }

    public String getValue(String key) {
        String s = valueOperations.get(key);
        log.info("获取 k : {} , v : {} ", key, s);
        return s;
    }

    public void testRead(String key) {
        ValueOperations ops = redisTemplate.opsForValue();
        Object o = ops.get(key);
        log.info("获取 k : {} , v : {} ", key, o);
    }

    public void testWrite(String key) {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(key, key);
        log.info("设置k : {} , v : {} ", key, key);
    }

}
